package com.gadgets.cm01.user.controller.impl;

import com.gadgets.cm01.common.GadgetsCm01Constant;
import com.gadgets.cm01.user.controller.IUserInfoOperationController;
import com.gadgets.cm01.user.dto.UserChangePwdDTO;
import com.gadgets.cm01.user.dto.UserInfoOperationDTO;
import com.gadgets.cm01.user.dto.UserInfoQueryDTO;
import com.gadgets.cm01.user.pojo.User;
import com.gadgets.cm01.user.service.IUserOperationService;
import com.gadgets.framework.common.dto.HttpBaseResp;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsException;
import com.gadgets.framework.common.util.DigestUtil;
import com.gadgets.framework.common.util.StringUtils;
import com.gadgets.framework.statusmach.pojo.StatusMachine;
import com.gadgets.framework.statusmach.util.StatusUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author UncleXiu
 * @version v1.0.0
 * @data 2020-09-17
 *
 */
@RestController
@RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/userManager")
public class UserInfoOperationController implements IUserInfoOperationController {

    @Autowired
    @Qualifier(value = "userOperationServiceImpl")
    private IUserOperationService userOperationService;

    /**
     * 新增用户信息
     *
     * @param request              请求信息
     * @param response             应答信息
     * @param userInfoOperationDTO 用户信息
     *
     * @return 标准应答
     *
     * @throws GadgetsException 异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/addUser.do")
    //@RoleAuthCheck(userType = {GadgetsConstant.USER_TYPE.SYS_MANAGER})
    @Override
    public HttpBaseResp<Object> addUserInfo(HttpServletRequest request, HttpServletResponse response, UserInfoOperationDTO userInfoOperationDTO) throws GadgetsException {
        User user = new User();
        user.setUserName(userInfoOperationDTO.getUserName());
        user.setUserType(userInfoOperationDTO.getUserType());
        user.setUserPassword(GadgetsCm01Constant.INIT_PWD_DIGEST);
        user.setPwdErrorTm(0);
        user.setUserStatus(GadgetsCm01Constant.USER_STATUS.USER_STATUS_INIT);
        userOperationService.addUserInfo(user);
        return new HttpBaseResp<>();
    }

    /**
     * 修改用户密码
     *
     * @param request          请求信息
     * @param response         应答信息
     * @param userChangePwdDTO 用户修改密码信息
     *
     * @throws GadgetsException 异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/changePwd.do")
    @Override
    public HttpBaseResp<Object> changeUserPwd(HttpServletRequest request, HttpServletResponse response, UserChangePwdDTO userChangePwdDTO) throws GadgetsException {
        userChangePwdDTO.setOperationType(GadgetsCm01Constant.USER_PWD_OPER_TYPE.CHANGE_PWD);
        userChangePwdDTO.setOldPassword(DigestUtil.digestBySHA2_512(userChangePwdDTO.getOldPassword()));
        userChangePwdDTO.setNewPassword(DigestUtil.digestBySHA2_512(userChangePwdDTO.getNewPassword()));
        userOperationService.updateUserPwdInfo(userChangePwdDTO);
        return new HttpBaseResp<>();
    }

    /**
     * 初始化用户密码
     *
     * @param request          请求信息
     * @param response         应答信息
     * @param userChangePwdDTO 用户修改密码信息
     *
     * @throws GadgetsException 异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/initUser.do")
    //@RoleAuthCheck(userType = {GadgetsConstant.USER_TYPE.SYS_MANAGER})
    @Override
    public HttpBaseResp<Object> initUserPwd(HttpServletRequest request, HttpServletResponse response, UserChangePwdDTO userChangePwdDTO) throws GadgetsException {
        userChangePwdDTO.setOperationType(GadgetsCm01Constant.USER_PWD_OPER_TYPE.RESET_PWD);
        userChangePwdDTO.setNewPassword(GadgetsCm01Constant.INIT_PWD_DIGEST);
        userOperationService.updateUserPwdInfo(userChangePwdDTO);
        return new HttpBaseResp<>();
    }

    /**
     * 删除用户信息
     *
     * @param request      请求信息
     * @param response     应答信息
     * @param operationDTO 用户修改密码信息
     *
     * @throws GadgetsException 异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/deleteUser.do")
    //@RoleAuthCheck(userType = {GadgetsConstant.USER_TYPE.SYS_MANAGER})
    @Override
    public HttpBaseResp<Object> deleteUserInfo(HttpServletRequest request, HttpServletResponse response, UserInfoOperationDTO operationDTO) throws GadgetsException {
        StatusMachine statusMachine = StatusUtil.getStatusMachine(GadgetsCm01Constant.CM01_STATUS_CODE.UPDATE_STATUS_MACHIN_CODE,operationDTO.getOperationType()+"");
        // 获取状态机，设置前置后后置状态
        operationDTO.setBeforeStatus(StringUtils.splitStrToInt(statusMachine.getBeforeStatus()));
        operationDTO.setAfterStatus(Integer.parseInt(statusMachine.getAfterStatus()));
        userOperationService.updateUserStatusInfo(operationDTO);
        return new HttpBaseResp<>();
    }

    /**
     * 查询用户
     *
     * @param request    请求信息
     * @param response   应答信息
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     *
     * @return 查询结果
     *
     * @throws GadgetsException 异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/queryUserInfo.do")
    //@RoleAuthCheck(userType = {GadgetsConstant.USER_TYPE.SYS_MANAGER})
    @Override
    public HttpBaseResp<User> queryUserInfoByCondition(HttpServletRequest request, HttpServletResponse response, UserInfoQueryDTO queryDTO, Pagination pagination) throws GadgetsException {
        List<User> users = userOperationService.queryUserInfoByCondition(queryDTO, pagination);
        return new HttpBaseResp<>(users,pagination);
    }

    /**
     * 操作用户状态
     *
     * @param request      请求信息
     * @param response     应答信息
     * @param operationDTO 操作信息
     *
     * @throws GadgetsException 异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/changeUserStatus.do")
    //@RoleAuthCheck(userType = {GadgetsConstant.USER_TYPE.SYS_MANAGER})
    @Override
    public HttpBaseResp<Object> operateUserStatus(HttpServletRequest request, HttpServletResponse response, UserInfoOperationDTO operationDTO) throws GadgetsException {
        StatusMachine statusMachine = StatusUtil.getStatusMachine(GadgetsCm01Constant.CM01_STATUS_CODE.UPDATE_STATUS_MACHIN_CODE,operationDTO.getOperationType()+"");
        // 获取状态机，设置前置后后置状态
        operationDTO.setBeforeStatus(StringUtils.splitStrToInt(statusMachine.getBeforeStatus()));
        operationDTO.setAfterStatus(Integer.parseInt(statusMachine.getAfterStatus()));
        userOperationService.updateUserStatusInfo(operationDTO);
        return new HttpBaseResp<>();
    }

}
